<?php

require_rewrite(MODULE_ROOT . "manager/admin/LogManager.class.php");
class ExaminationController extends BaseController {

    private $method;

    public function __construct() {
        parent::__construct();
        $this->method = Handler_http::getMethod();
    }

    /**
     * 审批策略管理列表
     */
    public function StrategyList() {
        $list = new ExaminModule();
         if($this->method =="POST"){
            $name= $_POST['rule_name'];
            $this->tpl->assign('name', $name);
        }
        $this->tpl->assign('countlist', count($list->selList()));
        $this->tpl->assign('list', $list->selList($name));
        $this->tpl->display("examination/exam_list.html");
    }


    /**
     * 添加审批策略
     */
    public function Strategy(){
        $id=$_GET['id'];
        $rule=new ExaminModule();
        if(!empty($id)){
            $details=$rule->selList('',$id);
            $this->tpl->assign('id', $id);
            $arr=array();
            $arr=explode(';',$details[0]['level']);
            $non=explode(',',$arr[0]);
            $internal=explode(',',$arr[1]);
            $secret=explode(',',$arr[2]);
            $con=explode(',',$arr[3]);
            $top=explode(',',$arr[4]);
            $this->tpl->assign('non', $non);
            $this->tpl->assign('internal', $internal);
            $this->tpl->assign('secret', $secret);
            $this->tpl->assign('con', $con);
            $this->tpl->assign('top', $top);
            $this->tpl->assign('details', $details);
        }
        if($this->method=="POST"){
            $hid = $_POST['id'];
            $rule_name = $_POST['rule_name'];
            $rule_type = $_POST['rule_type'];
            if (empty($rule_name)) {
                    Handler_tool::alertAndRedirect('请输入审批策略名称！', HTTPFILEURL . '/examination/Strategy/?id='.$id);
            }
            //非密
            $non_radio = $_POST['non_radio'];
            $non_rule = $_POST['non_rule'];
            //内部
            $internal_radio = $_POST['internal_radio'];
            $internal_rule = $_POST['internal_rule'];
            //秘密
            $secret_radio = $_POST['secret_radio'];
            $secret_rule = $_POST['secret_rule'];
            //机密
            $con_radio = $_POST['con_radio'];
            $con_rule = $_POST['con_rule'];
            //绝密
            $top_radio = $_POST['top_radio'];
            $top_rule = $_POST['top_rule'];
            //4非密,3内部,2秘密,1机密,0绝密
            $str='4,'.$non_radio.','.$non_rule.';3,'.$internal_radio.','.$internal_rule.';2,'.$secret_radio.','.$secret_rule.';1,'.$con_radio.','.$con_rule.';0,'.$top_radio.','.$top_rule.';';
            if (!empty($_POST['job']) && $_POST['job'] == 'job') {
                if ($rule->sqlList($rule_name,$rule_type,$str,$hid)) {
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '编辑审批策略' . $rule_name.'成功');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                    Handler_tool::alertAndRedirect('编辑成功！', HTTPFILEURL . '/examination/strategylist/');
                }else{
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '编辑审批策略' . $rule_name.'失败');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                    Handler_tool::alertAndRedirect('编辑失败！', HTTPFILEURL . '/examination/strategylist/?id=' . $hid);
                }
            } else {
                if($rule->sqlList($rule_name,$rule_type,$str)){
                     $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '添加审批策略' . $rule_name,'成功');
                     LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                     Handler_tool::alertAndRedirect('添加成功！', HTTPFILEURL . '/examination/strategylist/');
                }else{
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '添加审批策略' . $rule_name.'失败');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                    Handler_tool::alertAndRedirect('添加失败,请重新添加！', HTTPFILEURL . '/examination/strategylist/');
                }
            }
        }
        $this->tpl->display("examination/strategy.html");
    }

    /**
     * 删除审批管理列表
     */
    public function sqlDelList() {
        $id=$_GET['id'];
         if(!empty($id)){
             $list = new ExaminModule();
             $details=$list->selList('',$id);
             if ($list->SqlDel($id)) {
                $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '删除审批策略'.$details[0]['name'].'成功');
                LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                Handler_tool::alertAndRedirect('删除成功！', HTTPFILEURL . '/examination/strategylist/');
            }else{
                $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '删除审批策略'.$details[0]['name'].'失败');
                LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                Handler_tool::alertAndRedirect('删除失败！', HTTPFILEURL . '/examination/strategylist/');
            }
        }
    }

    /**
     * 审批流程管理列表
     */
    public function BatchList() {
         $list = new ExaminModule();
         if($this->method =="POST"){
            $name= $_POST['process_name'];
            $this->tpl->assign('process_name', $name);
        }
        $this->tpl->assign('list', $list->SqlBatch($name));
        $this->tpl->display("examination/barch_list.html");
    }

    /**
     * 添加编辑审批流程
     */
    public function addEditorProcess(){
        $list = new ExaminModule();
        $process=$list->selList();
        if(empty($process)){
            Handler_tool::alertAndRedirectTop('请先设置审批策略！', HTTPFILEURL.'/admin_index/');
        }
        $this->tpl->assign('process',json_encode($process));
        $this->tpl->assign('userlist',UserDao::sql_getExamine());
        $this->tpl->assign('groud', UserDao::getDepartmentList());
        $examin=new ExaminModule();
        $id=$_GET['id'];
        if(!empty($id)){
            $details=$examin->SqlBatch('',$id);
            $check_user_ids=explode(">",$details[0]['check_user_ids']);
            foreach($check_user_ids as $key=>$r){
                if(strpos($r,"|") !==false){
                    $arr[$key]= explode('|',$r);
                }else{
                    $arr[$key]= $r;
                }
            }
            $html='';
           $num=1;
            foreach($arr as $row ){
                if(is_array($row)){
                    $html.="<tr><td width='100%'>";
                    $html.="<fieldset style='height:100%;'>";
                    $html.="<legend>".$num."级审批</legend>";
                    $html.="<table border='0' cellpadding='2' cellspacing='1' style='width:100%'>";
                    $html.="<tr><td nowrap align='right' width='10%'>审批人:</td>";
                    $html.="<td width='35%'>";
                    $html.="<table border='1' cellpadding='2' cellspacing='1' style='width:50%'>";
                    $userlist=UserDao::sql_getExamine();
                        foreach ($userlist as $list){
                            $html.="<tr><td>";
                            $html.="<input type='checkbox' name=checkbox_".$num." value=".$list['id'];
                            foreach($row as $r){
                                if($list['id'] == $r){
                                    $html.=" checked='checked'";
                                }
                            }
                            $html.="></td>";
                            $html.="<td>".$list['username']."</td><td>".$list['group_nickname']."</td>";
                            $html.="</tr>";
                        }
                    $html.="</table></td></tr></table></fieldset></td></tr>";
                }else{
                    $html.="<tr><td width='100%'>";
                    $html.="<fieldset style='height:100%;'>";
                    $html.="<legend>".$num."级审批</legend>";
                    $html.="<table border='0' cellpadding='2' cellspacing='1' style='width:100%'>";
                    $html.="<tr><td nowrap align='right' width='10%'>审批人:</td>";
                    $html.="<td width='35%'>";
                    $html.="<table border='1' cellpadding='2' cellspacing='1' style='width:50%'>";
                    $userlist=UserDao::sql_getExamine();
                        foreach ($userlist as $list){
                            $html.="<tr><td>";
                            if($list['id'] == $row){
                                $html.="<input type='checkbox' name=checkbox_".$num." value=".$list['id']." checked='checked'></td>";
                            }else{
                                $html.="<input type='checkbox' name=checkbox_".$num." value=".$list['id']."></td>";
                            }
                            $html.="<td>".$list['username']."</td><td>".$list['group_nickname']."</td>";
                            $html.="</tr>";
                        }
                    $html.="</table></td></tr></table></fieldset></td></tr>";
                    }
                    $num++;
                }
            }
            $this->tpl->assign('html',$html);
            $this->tpl->assign('count_ids',count($check_user_ids));
            $this->tpl->assign('hid',$id);
            $this->tpl->assign('details',$details);
        if($this->method=="POST"){
            $hid = $_POST['id'];
            $array['process_name']=$_POST['process_name'];
            $array['rule_type']=$_POST['rule_type'];
            $array['groudid']=$_POST['groud'];
            $array['levelId']=$_POST['levelId'];
            $array['level_userid']=$_POST['level_userid'];
            if($array['levelId']==5){
                Handler_tool::alertAndRedirect('请选择密级进行添加！', HTTPFILEURL.'/examination/addEditorProcess/');
            }
            if (!empty($_POST['job']) && $_POST['job'] == 'job') {
                if ($examin->upSqlBatch($array,$hid)) {
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '编辑审批流程' .$array['process_name'].'成功');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                    Handler_tool::alertAndRedirect('编辑成功！', HTTPFILEURL . '/examination/BatchList/');
                }else{
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '编辑审批流程' .$array['process_name'].'失败');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                    Handler_tool::alertAndRedirect('编辑失败！', HTTPFILEURL . '/examination/addEditorProcess/?id=' . $hid);
                }
            }else{
                if($examin->addSqlBatch($array)){
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '添加审批流程' .$array['process_name'].'成功');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                    Handler_tool::alertAndRedirect('添加审批流程成功！', HTTPFILEURL . '/examination/BatchList/');
                }else{
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '添加审批流程' .$array['process_name'].'失败');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 2, 2);
                    Handler_tool::alertAndRedirect('添加失败,请重新添加！', HTTPFILEURL . '/examination/BatchList/');
                }
            }
        }
        $this->tpl->display("examination/add_process.html");
    }

     /**
     * 删除审批流程
     */
    public function sqlDelBatchList() {
        $id=$_GET['id'];
         if(!empty($id)){
             $list = new ExaminModule();
             $details=$list->selList('',$id);
             if ($list->SqlBatchDel($id)) {
                $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '删除审批策略'.$details[0]['name'].'成功');
                LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                Handler_tool::alertAndRedirect('删除成功！', HTTPFILEURL . '/examination/batchList/');
            }else{
                $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '删除审批策略'.$details[0]['name'].'失败');
                LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                Handler_tool::alertAndRedirect('删除失败！', HTTPFILEURL . '/examination/batchList/');
            }
        }
    }
}

?>